package com.example.vertification.controller;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.example.vertification.Utils.RedisUtil;
import com.example.vertification.entity.Question;
import com.example.vertification.entity.User;
import com.example.vertification.entity.UserStatics;
import com.example.vertification.service.QuestionService;
import com.example.vertification.service.UserService;
import com.example.vertification.service.UserStaticsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@RestController
@Api(value = "拼图小游戏", tags = "拼图小游戏")
public class PuzzleController {

    private final UserService userService;


    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    public PuzzleController(UserService userService) {
        this.userService = userService;
    }



    @GetMapping("/final_todays_puzzle")
    @ApiOperation(value = "传入类型为对象token，进行完成每日拼图小游戏记录")
    public ResponseEntity<?> final_todays_puzzle(@RequestParam(required = false) String token) throws IOException {
        User u = userService.getUserByToken(token);
        if(u==null){
            return ResponseEntity.ok("请先登入!");
        }
        if(redisUtil.canPuzzle(u.getUid().toString())==false) {
            return ResponseEntity.ok("今日游戏次数达到上限,请明日再来游戏!");
        }
        redisUtil.finalPuzzle(u.getUid().toString());
        u.setScore(u.getScore()+10);
        userService.saveUser(u);
        return ResponseEntity.ok("拼图成功,积分+10点");
    }


    @GetMapping("/can_i_puzzle")
    @ApiOperation(value = "传入类型为对象token，进行完成每日拼图小游戏能否进行的判定")
    public ResponseEntity<?> can_i_puzzle(@RequestParam(required = false) String token) throws IOException {
        User u = userService.getUserByToken(token);
        if(u==null){
            return ResponseEntity.ok("请先登入!");
        }
        if(redisUtil.canPuzzle(u.getUid().toString())==false) {
            return ResponseEntity.ok("今日游戏次数达到上限,请明日再来游戏!");
        }

        return ResponseEntity.ok("开始拼图,如果太难可以重新开始~");
    }



}
